﻿var EmployeeInfoPage = function(pageContainer) {

    EmployeeInfoPage.superclass.constructor.call(this, pageContainer);

    var self = this;
    var currentPra_id = null;
    var situatieCO = null;
    var Elements = {
        topSection: pageContainer.find(".employeeTop"),
        situatieCOContainer: pageContainer.find(".situatieCOContainer"),
        emplImg: pageContainer.find(".employeeImg"),
        emplName: pageContainer.find(".employeeName"),
        emplFctName: pageContainer.find(".employeeFctName"),
        emplDepName: pageContainer.find(".employeeDepName"),
        emplCNP: pageContainer.find(".employeeCNP"),
        emplMarca: pageContainer.find(".employeeMarca")
    };

    var employee = null;

    this.Actions = [
        {
            Text: 'None',
            Callback: function() {},
            CanExecute: function() { return true; }
        }
    ];

    self.initialize = override(self.initialize, function() {
        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(true);
        });

        pageContainer.bind('onPageShow', function() {
        });

        pageContainer.bind('onPageHide', function() {
            if (situatieCO)
                situatieCO.hideContainer();
        });
    });

    this.refresh = function(pra_id) {
        if (pra_id != currentPra_id) {
            currentPra_id = pra_id;
            app.TMMService.General.GetSubordinateInfo(pra_id, function(response) {
                if (response && response.FinalResult) {
                    employee = response.FinalResult;
                    if (employee.HasPicture) {
                        Elements.emplImg.attr("src", Utils.getEmployeePhoto(response.FinalResult.PRS_ID));
                    } else Elements.emplImg.attr("src", Utils.getEmployeePhoto(0, response.FinalResult.CNP));

                    Elements.emplName.html(response.FinalResult.Name);
                    Elements.emplFctName.html(response.FinalResult.FNC_NUME);
                    Elements.emplDepName.html(translate('Cerere_Departament') + " " + response.FinalResult.DPT_NUME);
                    Elements.emplCNP.html(translate('Cerere_CNP') + " " + response.FinalResult.CNP);
                    Elements.emplMarca.html(translate('EmployeeInfo_Marca') + " " + response.FinalResult.Marca);

                    initializeSituatieCO(currentPra_id);
                    app.translatePage(app.Pages.EmployeeInfo);
                } else app.MessagesFactory.showToastWarning(translate('General_Eroare'), translate('General_EroareServer'));
            });
        } else situatieCO.refresh(false);
    };
    var initializeSituatieCO = function(pra_id) {
        if (!situatieCO)
            situatieCO = new SituatieCO(Elements.situatieCOContainer);

        situatieCO.setEmployee({
            Id: pra_id,
            Name: employee.Name
        });

        Elements.situatieCOContainer.show();
    };

    this.setModuleAction = override(self.setModuleAction, function() {
        if (app.Phone)
            app.Phone.setModuleAction(ModuleActions.None);
    });

    self.initialize();
};
extend(EmployeeInfoPage, TMMobilePage);